package com.multi.thread.chapter04.demo11;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *	awaitUninterruptibly():使当前线程在接收到信号之前一直处于等待状态
 */
public class TestAwaitUninterruptibly {
	
	private ReentrantLock lock = new ReentrantLock();
	private Condition condition  = lock.newCondition();
	
	public void test() {
		try {
			lock.lock();
			System.out.println("wait begin, " + System.currentTimeMillis());
			condition.awaitUninterruptibly();
			System.out.println("wait end, " + System.currentTimeMillis());
		} finally {
			lock.unlock();
		}
	}
	
	public void testSignal() {
		try {
			System.out.println("signal time : " + System.currentTimeMillis());
			lock.lock();
			condition.signal();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		final TestAwaitUninterruptibly service = new TestAwaitUninterruptibly();
		Runnable run = new Runnable() {
			@Override
			public void run() {
				service.test();
			}
		};
		Thread thread = new Thread(run);
		thread.start();
		Thread.sleep(3000);
		thread.interrupt();
		
		Runnable signal = new Runnable() {
			@Override
			public void run() {
				service.testSignal();
			}
		};
		Thread signalThread = new Thread(signal);
		signalThread.start();
	}

}
